// package1/pages/person/person.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gunId: '',
    time: '',
    visibel: false,
    info: {},
    start: "",
    waitSn: '',
    chargeOrder:{},
    gunList: [
    ],
    islogin: true,
    visible: false,
    agreement: '',
    pickerShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(wx.getStorageSync('token')){
      this.setData({
        islogin: true,
      })
      this.getList()
    }else{
      this.setData({
        islogin: false
      })
      this.goLogin()
    }
  },
  goLogin(){
    wx.navigateTo({
      url: '/subpage/pages/login/login',
    })
  },
  comboRule(){
    wx.navigateTo({
      url: '/package1/pages/person/combo',
    })
  },
  cancelWait(e){
    let that = this
    service("GET","/personal/pile/order/cancel/"+e.currentTarget.dataset.sn).then(res => {
      if(res.data.code ==0){
        wx.showToast({
          title: '取消成功',
          icon: 'none'
        })
        that.getList()
      }else{
        wx.showToast({
          title: '操作失败',
          icon: 'none'
        })
      }
     })
  },
  getList(type = 2){
    service("GET","/personal/gun/owner").then(res => {
      if(res.data.code ==0){
        this.setData({
          gunList : res.data.pile
        })
      }
      if(type ==1){
        wx.stopPullDownRefresh()
      }
     })
  },
  orderDetail(e) {
    console.log('传输',e)
    wx.navigateTo({
      url: '/package1/pages/order/order',
    })
  },
  orderList(){
    wx.navigateTo({
      url: '/package1/pages/order/order',
    })
  },
  chooseGun(e){
    console.log('e',e.currentTarget.dataset.id)
    let id = e.currentTarget.dataset.id
    this.setData({
      gunId: id 
    })
  },

  apply(){
    wx.navigateTo({
      url: '/package1/pages/apply/index',
    })
  },
  //开始充电
  startCharge(e){
    console.log(e)
    let that = this
    let online = e.currentTarget.dataset.online
    let sn = e.currentTarget.dataset.sn
    if(!online){
      wx.showToast({
        title: '充电枪离线，不能进行此操作',
        icon: 'none'
      })
      return
    }
    service("GET","/personal/pile/start/"+sn).then(res => {
      console.log('开始充电', res.data.oid, sn)
      if(res.data.code ==0){
        wx.navigateTo({
          url: '/package1/pages/order/personal?sn='+sn+'&oid='+res.data.oid,
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
      
    })
  },
  stopCharge(e){
    let sn = e.currentTarget.dataset.sn
    let that = this
    service("GET","/personal/pile/stop/"+sn).then(res => {
      console.log('停止充电', res)
      if(res.data.code ==0){
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
       setTimeout(() => {
        that.getList()
       }, 10000);
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
      
    })
  },
  personOrder(){
    wx.navigateTo({
      url: '/package1/pages/order/order',
    })
  },
  myPile(){
    wx.navigateTo({
      url: '/package1/pages/pile/list',
    })
  },
  repair(e){
    let gunId = e.currentTarget.dataset.sn
    wx.navigateTo({
      url: '/package1/pages/repair/index?gunId='+gunId,
    })
  },
  onClose(){
    this.setData({
      visibel: false,
      waitSn: ''
    })
  },
  showWait(e){
    console.log(e)
    let online = e.currentTarget.dataset.online
    let sn = e.currentTarget.dataset.sn
    // if(!online){
    //   wx.showToast({
    //     title: '充电桩离线，不能进行此操作',
    //     icon: "none"
    //   })
    //   return
    // }
    this.setData({
      pickerShow: true,
      waitSn: sn
    })
  },
  bindTimeChange(e){
    console.log(e)
    this.setData({
      time: e.detail.value
    })
  },
  personOrder(){
    wx.navigateTo({
      url: '/package1/pages/order/order',
    })
  },
  myCombo(e){
    let gunId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/package1/pages/combo/index?gunId='+gunId,
    })
  },
  // repair(){
  //   wx.navigateTo({
  //     url: '/package1/pages/repair/index',
  //   })
  // },
  onClose() {
    this.setData({ visible: false });
  },
  agreement(e){
    service('POST', '/personal/apply/agreement/view').then((res) => {
      console.log('协议', res)
      let data = res.data
      this.setData({
        agreement: data.html,
        visible: true
      })
     
    })
    let sn = e.currentTarget.dataset.sn
    // wx.navigateTo({
    //   url: '/package1/pages/pile/pile?gunId='+sn,
    // })
  },
  publiShip(e){
    let sn = e.currentTarget.dataset.sn
    wx.navigateTo({
      url: '/package1/pages/person/pub?sn='+sn,
    })
  },
  //开始充电
  charge(e){
    let that = this
    console.log(e)
    let sn = e.currentTarget.dataset.id
    if(!this.data.info.online){
      wx.showToast({
        title: '充电枪离线，不能进行此操作',
        icon: 'none'
      })
      return
    }
    service("GET","/personal/pile/start/"+sn).then(res => {
     console.log('开始充电', res)
     if(res.data.code ==0){
       wx.navigateTo({
         url: '/package1/pages/order/personal?oid='+res.data.oid,
       })
     }else{
       wx.showToast({
         title: res.data.msg,
         icon: 'none'
       })
     }
     
    })
  },
  confirmTime(option){
    console.log('confirmTime',option)
    
  },
      //预约充电
  waitCharge(e){
    console.log(e)
    let data = e.detail
    console.log('time',  e.detail)
    let sn = e.currentTarget.dataset.sn
    let that = this
    // let data = e.detail.value
    if(!data){
      wx.showToast({
        title: '请选择预约时间',
        icon: 'none'
      })
      return 
    }
    let strs=data.split(":")
    console.log('strs',strs)
    let date = strs[0]
    let hour = strs[1]>= 10 ? strs[1] : '0'+strs[1]
    let minute = strs[2] >=10 ? strs[2]: '0'+strs[2]
    let second = '00'
    let time = date+ ' '+hour +':'+ minute +':'+ second
    console.log('time',time)
    service("POST","/personal/pile/order", { time, sn }).then(res => {
      console.log('预约充电', res)
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      that.setData({
        waitSn: ''
      })
      
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log()
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})